﻿#include "versionsform.h"
#include "ui_versionsform.h"

#include <QFile>
#include <QGraphicsDropShadowEffect>
#include <QSettings>
#include <QTextCodec>
#include <QTextStream>

VersionsForm::VersionsForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VersionsForm)
{
    ui->setupUi(this);
    ui->tableWidget->setShowGrid(true);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

    //自动调整窗口列表的【宽、高】---适应窗口大小
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->tableWidget->verticalHeader()->hide();//取消默认行号
    QStringList header;
    header << tr("param")<< tr("versions");
    ui->tableWidget->setColumnCount(header.size());
    ui->tableWidget->setHorizontalHeaderLabels(header);
    UpVersionInfo();


    QGraphicsDropShadowEffect *shadow_effectA = new QGraphicsDropShadowEffect(this);
    QGraphicsDropShadowEffect *shadow_effectB = new QGraphicsDropShadowEffect(this);
    shadow_effectA->setOffset(0, 0);
    shadow_effectA->setColor(QColor(150,150,150));
    shadow_effectA->setBlurRadius(6);

    shadow_effectB->setOffset(0, 0);
    shadow_effectB->setColor(QColor(150,150,150));
    shadow_effectB->setBlurRadius(6);

    ui->versions_gBox_1->setGraphicsEffect(shadow_effectA);
    ui->versions_gBox_2->setGraphicsEffect(shadow_effectB);

    ui->tableWidget->setAlternatingRowColors(true);
}

VersionsForm::~VersionsForm()
{
    delete ui;
}

void VersionsForm::UpVersionInfo()
{
    QString Path = "configs/version.ini";
    QSettings iniRead(Path, QSettings::IniFormat);
    iniRead.setIniCodec(QTextCodec::codecForName("UTF_8"));



    QString major = iniRead.value("major_version/part_1").toString();

    ui->lineEdit->setText(major);
    int nNum = iniRead.value("devices/num").toInt();
    ui->tableWidget->setRowCount(nNum); // 设置显示的行数

    QString info;

    for (int i=0;i<nNum;i++) {
        info = QString("device/part_%1").arg(i+1);
        QTableWidgetItem* Item = new QTableWidgetItem(iniRead.value(info).toString());
        Item->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        ui->tableWidget->setItem(i, 0, Item);


        info = QString("device_info/part_%1").arg(i+1);
        QTableWidgetItem* Item2 = new QTableWidgetItem(iniRead.value(info).toString());
        Item2->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        ui->tableWidget->setItem(i, 1, Item2);
    }
}
